package com.wcity.manage.web.property;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.PayTypeMapper;
import com.wcity.manage.dao.model.Payment;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.property.PayTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 缴费类型模块
 * Created by zm on 2016/12/12.
 */
@Controller
public class PayTypeAct {

    @Resource private PayTypeService payTypeService;
    @Resource private PayTypeMapper payTypeMapper;

    /**
     * 缴费类型分页查询
     * @param payment
     * @param pageModel
     * @param request
     * @return
     * @Author zm
     */
    @RequestMapping("payType_list")
    public String payTypeList (Payment payment , PageModel pageModel, HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        payment.setUserId(user.getId());
        pageModel.setParams( payment );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "PayType", payment );
        request.setAttribute( "userId", user.getId() );
        request.setAttribute( "list", payTypeService.getPayTypeListPage( pageModel ) );
        return "payment/payType";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("to_payType_add")
    public String toPayTypeAdd () {
        return "payment/payTypeAdd";
    }

    /**
     * 添加
     * @param payment
     * @param request
     * @param response
     */
    @RequestMapping("payType_add")
    public void payTypeAdd (Payment payment , HttpServletRequest request, HttpServletResponse response ) {
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        payment.setUserId(user.getId());
        payment.setCreateTime( new Date());
        String code = "200";
        String meg = "添加成功";
        try{
            int count = payTypeMapper.getPayTypeCount ( payment.getPaymentType() , user.getId());
            if( count >0 ){
                code = "300";
                meg = "添加失败,缴费类型重复";
            }else {
                int success = payTypeService.insertPayType(payment);
                if (success == 0) {
                    code = "300";
                    meg = "添加失败";
                }
            }
        }catch (Exception e) {
            code = "300";
            meg = "添加失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "payType_list", "", "closeCurrent", ""));
    }

    /**
     * 跳转到更新页面
     * @param request
     * @return
     */
    @RequestMapping("to_payType_edit")
    public String toPayTypeEdit ( HttpServletRequest request ){
        String id = request.getParameter("id");
        request.setAttribute( "list", payTypeService.getPayTypeInfo( id ) );
        return "payment/payTypeEdit";
    }

    /**
     * 修改
     * @param payment
     * @param response
     * @param
     */
    @RequestMapping("payType_edit")
    public void payTypeEdit (Payment payment, HttpServletResponse response  ) {

        String code = "200";
        String meg = "修改成功";
        try{
            int success = payTypeService.updateUser(payment);
            if( success == 0 ){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "payType_list", "", "closeCurrent", ""));
    }

    /**
     * 删除
     * @param request
     */
    @RequestMapping("payType_delect")
    public void payTypeDelect (HttpServletRequest request,HttpServletResponse response) {

        String code = "200";
        String meg = "删除成功";
        try{
            String id = request.getParameter("id");
            int success = payTypeService.payTypeDelect(id);
            if( success == 0 ){
                code = "300";
                meg = "删除失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "删除失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "payType_list", "", "", ""));
    }
}
